package com.vip8.recorder.service.utils;

import org.springframework.cglib.beans.BeanCopier;

/**
 * Bean Copy 工具类
 */
public class BeanCopyUtil {

    /**
     * 实例拷贝
     *
     * @param source 原实例
     * @param target 目标实例
     */
    public static <S, T> void copyBean(S source, T target) {
        if (source != null && target != null) {
            BeanCopier copier = BeanCopier.create(source.getClass(), target.getClass(), false);
            copier.copy(source, target, null);
        }
    }

    /**
     * 实例拷贝
     *
     * @param source 原实例
     * @param target 目标类
     * @return
     */
    public static <S, T> T getBean(S source, Class<T> target) {
        if (source == null) {
            return null;
        }
        T temp = null;
        try {
            temp = target.newInstance();
        } catch (Exception e) {
            throw new RuntimeException("BeanCopyUtil.getBean Error: " + JsonUtil.toJsonString(source));
        }
        copyBean(source, temp);
        return temp;
    }
}
